﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using IMC.Prepare.Web.Models.Customer;
using IMC.Prepare.Services.Localization;
using IMC.Prepare.Services.Customers;
using IMC.Prepare.Services.Authentication;

namespace IMC.Prepare.Web.Controllers
{
    public class CustomerController : Controller
    {
        private readonly IAuthenticationService _authenticationService;
        private readonly ICustomerService _customerService;
        private readonly ILocalizationService _localizationService;

        public CustomerController(
            IAuthenticationService authenticationService, 
            ICustomerService customerService,
            ILocalizationService localizationService)
        {
            this._authenticationService = authenticationService;
            this._customerService = customerService;
            this._localizationService = localizationService;
        }

        public ActionResult Login(bool? checkoutAsGuest)
        {
            var model = new LoginModel();
            //model.UsernamesEnabled = _customerSettings.UsernamesEnabled;
            model.UsernamesEnabled = true;
            model.CheckoutAsGuest = checkoutAsGuest.HasValue ? checkoutAsGuest.Value : false;
            return View(model);
        }


        [HttpPost]
        public ActionResult Login(LoginModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (model.Username != null)
                {
                    model.Username = model.Username.Trim();
                }

                if (_customerService.ValidateCustomer(model.Username , model.Password))
                {
                    var customer =  _customerService.GetCustomerByUsername(model.Username);

                    //migrate shopping cart
                    //_shoppingCartService.MigrateShoppingCart(_workContext.CurrentCustomer, customer);

                    //sign in new customer
                    _authenticationService.SignIn(customer, model.RememberMe);


                    if (!String.IsNullOrEmpty(returnUrl) && Url.IsLocalUrl(returnUrl))
                        return Redirect(returnUrl);
                    else
                        return RedirectToAction("Index", "Blog");
                }
                else
                {
                    ModelState.AddModelError("", _localizationService.GetResource("Account.Login.WrongCredentials"));
                }
            }

            //If we got this far, something failed, redisplay form
            model.UsernamesEnabled = true;
            return View(model);
        }

        public ActionResult Logout()
        {
            //external authentication
            //ExternalAuthorizerHelper.RemoveParameters();

            //if (_workContext.OriginalCustomerIfImpersonated != null)
            //{
            //    //logout impersonated customer
            //    _customerService.SaveCustomerAttribute<int?>(_workContext.OriginalCustomerIfImpersonated,
            //        SystemCustomerAttributeNames.ImpersonatedCustomerId, null);
            //    //redirect back to customer details page (admin area)
            //    return this.RedirectToAction("Edit", "Customer", new { id = _workContext.CurrentCustomer.Id, area = "Admin" });

            //}
            //else
            //{
                //standard logout 
                _authenticationService.SignOut();
                return this.RedirectToAction("Index", "Blog");
            //}

        }
    }
}
